<template>
  <div id="clockin">
    <el-breadcrumb>
      <el-breadcrumb-item>个人中心</el-breadcrumb-item>
      <el-breadcrumb-item>我的考勤</el-breadcrumb-item>
      <el-breadcrumb-item>打卡记录</el-breadcrumb-item>
    </el-breadcrumb>
    <div id="main">
      <el-form :inline="true" :model="searchData" class="demo-form-inline">
        <el-form-item label="时间">
          <div class="block">
            <el-date-picker
              v-model="searchData.time"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <div id="line"></div>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="mydate" label="日期" width="300px">
        </el-table-column>
        <el-table-column prop="myhour" label="打卡记录"> </el-table-column>
      </el-table>
      <div class="block block2">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="count"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalpage"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      searchData: {
        time: "",
      },
      searchData2: [],
      totaldata: [],
      currentPage4: 1,
      count: 5,
      mypage: "",
      mytime: [],
    };
  },
  methods: {
    onSubmit() {
      this.currentPage4 = 1;
      if (this.searchData.time) {
        let time = this.searchData.time ? this.searchData.time : "";
        this.page = 1;
        this.searchData2 = [time];
        // console.log(this.searchData2);
      } else {
        this.searchData = "";
      }
    },
    reset() {
      this.currentPage4 = 1;
      this.show();
      this.totaldata = [];
      this.searchData = {};
      this.searchData2 = [];
    },
    handleSizeChange(n) {
      this.count = n;
    },
    handleCurrentChange(n) {
      this.currentPage4 = n;
    },
    show() {
      // console.log(this.$store.state.data.jobs);
      this.$axios({
        url: "/personalcenter/clockin/get",
        params: { myid: JSON.parse(sessionStorage.getItem("user")).work_id },
      }).then((res) => {
        res.data.data.forEach((v, i) => {
          v.mydate = v.clock_in.substring(0, 10);
          let h1 = v.clock_in.substring(10, 20);
          let h2 = v.clock_go.substring(10, 20);
          v.myhour = h1 + " " + h2;
          console.log(v.myhour);
          this.totaldata.push(v);
        });
      });
    },
  },
  created() {
    this.show();
  },
  computed: {
    totalpage: function () {
      console.log(this.searchData2);
      if (this.searchData2.length != 0) {
        return this.mypage;
      } else {
        return this.totaldata.length;
      }
    },
    tableData: function () {
      // console.log(totaldata);
      if (this.searchData2.length != 0) {
        let arr = [];
        this.totaldata.forEach((v, i) => {
          if (
            new Date(v.clock_in) >= new Date(this.searchData2[0][0]) &&
            new Date(v.clock_in) <= new Date(this.searchData2[0][1])
          ) {
            arr.push(v);
          }
        });
        this.mypage = arr.length;
        return arr.slice(
          (this.currentPage4 - 1) * this.count,
          this.currentPage4 * this.count
        );
      } else {
        return this.totaldata.slice(
          (this.currentPage4 - 1) * this.count,
          this.currentPage4 * this.count
        );
      }
    },
  },
};
</script>
<style lang="less" scoped>
#mymain {
  margin-top: 20px;
}
#main {
  margin-top: 20px;
  height: 70%;
  background-color: white;
  height: 100%;
  padding: 20px;
}
.el-menu {
  padding-left: 20px;
}
.el-form-item {
  margin-right: 20px;
}
#line {
  width: 100%;
  height: 0.1px;
  background-color: #e5e5e5;
  margin-bottom: 20px;
}
#process {
  margin-bottom: 20px;
}
.block2 {
  margin-top: 30px;
  text-align: right;
}
</style>
